<?php
echo date_default_timezone_get();
date_default_timezone_set('Asia/Shanghai');
echo '<br/>set default timezone:',date_default_timezone_get();

echo "<br/><br/>date的使用";
echo "<br/>当前时间time()、strtotime('now'): ". date('c', strtotime('now'));
echo "<br/>当前时间date('c'): ". date('c');
echo "<br/> date('c') ISO 8601 日期: " . date('c');
echo "<br/>标准表示方法 Ymd H:i:s   " . date('c', strtotime(date('Ymd H:i:s')));
echo "<br/>年月日ymd " . date('ymd');
echo "<br/>date('t')计算这个月有多少天 t " . date('t');
echo "<br/>星期几 N " . date('N');
echo "<br/>月份n " . date('n');
echo "<br/>当年第几周 W" . date('W');
echo "<br/>一年中第几天 z " . date('z');
echo "<br/>" . date('Y-m-d H:i:s');
echo "<br/>01 M Y: " . date('01 M Y');
echo "<br/>ISO 8601 日期 c " . date('c', strtotime('2023-01-01 -1 day')); 
echo " 第几周 W:" . date('W', strtotime('2023-01-01 -1 day'));
echo " 周几 W:" . date('l', strtotime('2023-01-01 -1 day'));

echo "<br/>ISO 8601 日期 c " . date('c', strtotime('2023-01-01'));
echo " 第几周 W:" . date('W', strtotime('2023-01-01'));


echo "<br/>ISO 8601 日期 c " . date('c', strtotime('2023-01-02'));
echo " 第几周 W:" . date('W', strtotime('2023-01-02'));

echo "<br/>-1 day:  " . date('c', strtotime('-1 day'));
echo "<br/>-1 days: " . date('c', strtotime('-1 days'));
echo "<br/>+1 day:  " . date('c', strtotime('+1 day'));
echo "<br/><br/>+2 days today = today +2 days";
echo "<br/>+2 days today: " . date('c', strtotime('+2 days today'));
echo "<br/>today +2 days " . date('c', strtotime('today +2 days'));

echo "<br/>now:". date('c', strtotime('now')), "</br>";
echo "<br/>noon: " . date('c', strtotime('noon'));
echo "<br/>tomorrow: " . date('c', strtotime('tomorrow'));
echo "<br/>yestday: " . date('c', strtotime('yesterday'));

echo "<br/><br/>当前天开始";
echo "<br/> 00:00 = today = 0.0";
echo "<br/>today: " . date('c', strtotime('today'));
echo "<br/>midnight: " . date('c', strtotime('midnight'));
echo "<br/>00:00: " . date('c', strtotime('00:00'));
echo "<br/>0.0: " . date('c', strtotime('0.0'));
echo "<br/>某天2021-11-05的开始: " . date('c', strtotime('2021-11-05 midnight'));
echo "<br/>2015-01-02 +1 day:". date('c', strtotime('2015-01-02 +1 day')), "</br>";
echo "<br/>23:59:59: " . date('c', strtotime('23:59:59'));
echo "<br/>15:40:59: " . date('c', strtotime('15:40:59'));
echo "<br/>today -2 days: " . date('c', strtotime('today -2 days'));
echo "<br/>-1 day 00:00: " . date('c', strtotime('-1 day 00:00'));
echo "<br/>-3 day noon: " . date('c', strtotime('-3 day noon'));
echo "<br/>20200320 " . date('c', strtotime('20200320'));
echo "<br/>20200320 +1 day" . date('c', strtotime('20200320 +1 day'));
echo "<br/>midnight -3 day noon: " . date('c', strtotime('midnight -3 day noon'));

echo "<br/><br/>";
echo "<br/>Ymd H:i:s   " . date('c', strtotime(date('Ymd H:i:s')));
echo "<br/>date('Y-m-d H:i:s') 00:00 无效" . date('c', strtotime(date('Y-m-d H:i:s') . ' 00:00'));
echo "<br/>date('Y-m-d H:i:s') midnight" . date('c', strtotime(date('Y-m-d H:i:s') . ' midnight'));



echo "<br/>2018-12-03: " . date('c', strtotime('2018-12-03'));

echo "<br/><br/>月份";
echo "<br/>ordinal space dayname space 'of'   first day单用无效";
echo "<br/>当月第一天 Y-m-01: " . date('c', strtotime(date('Y-m-01')));
echo "<br/>first day of this month: " . date('c', strtotime('first day of this month'));
echo "<br/>first day of this month  00:00:00  : " . date('c', strtotime('first day of this month 00:00:00'));
echo "<br/>first day of this month midnight: " . date('c', strtotime('first day of this month midnight'));
echo "<br/>last day of this month: " . date('c', strtotime('last day of this month midnight'));
echo "<br/>ordinal space unit";
echo "<br/>上个月 last month: " . date('c', strtotime('last month'));
echo "<br/>second month:". date('c', strtotime('second month')), "</br>";
echo "<br/>twelfth month:". date('c', strtotime('twelfth month')), "</br>";
echo "<br/>first day of last month: " . date('c', strtotime('first day of last month midnight'));
echo "<br/>last day of last month: " . date('c', strtotime('last day of last month midnight'));

echo "<br/><br/>reltext space 'week' 单用week无效";
echo "<br/>monday 和monday this week 不是同一天";
echo "<br/>monday: " . date('c', strtotime('monday'));
echo "<br/>Monday  this week: " . date('c', strtotime(' monday  this week'));

echo "<br/>tuesday: " . date('c', strtotime('tuesday'));
echo "<br/>week: " . date('c', strtotime('week'));
echo "<br/>date('W') week: " . date('W');
echo "<br/>this week: " . date('c', strtotime('this week'));
echo "<br/>previous week: " . date('c', strtotime('previous week'));
echo "<br/>last week: " . date('c', strtotime('last week'));


echo "<br/>sunday this week: " . date('c', strtotime('sunday this week'));
echo "<br/>today this week: " . date('c', strtotime('today this week'));
echo "<br/>today -1 week: " . date('c', strtotime('today -1 week'));

echo "<br/>today previous week: " . date('c', strtotime('today previous week'));
echo "<br/>today last week: " . date('c', strtotime('today last week'));
echo "<br/>Monday previous week: " . date('c', strtotime('Monday previous week'));
echo "<br/>Monday last week: " . date('c', strtotime('Monday last week'));
echo "<br/>last Sat 上一个周六: " . date('c', strtotime('last Sat'));
echo "<br/>last Sat of this month 这个月最后一个周六" . date('c', strtotime('last Sat of this month'));
echo "<br/>last Sat  this month 实际是 last Sat " . date('c', strtotime('last Sat  this month'));
echo "<br/>noon -1 week " . date('c', strtotime('noon -1 week'));
echo "<br/>weekdays " . date('c', strtotime('weekdays'));
echo "<br/>-1 weekdays " . date('c', strtotime('-1 weekdays'));
echo "<br/>-2 weekdays " . date('c', strtotime('-2 weekdays'));
echo "<br/>-7 weekdays " . date('c', strtotime('-7 weekdays'));


echo "<br/><br/>11位数的秒数 代表到多少年呢";
echo "<br/>11位数的秒数代表哪一年:", date('c', 1*1E10);
echo "<br/>当时时间戳:" .time();

echo "<br/><br/>日期差 2023-10-20 : ";
$day1 = '2023-10-20';  //不要包含 2024-10-20 23:19:00
$curDay = date('Ymd');
$d1 = new DateTime($day1);
$d2 = new DateTime($curDay);
$diffDay = $d1->diff($d2)->days;    //距离curDay的天使
echo $diffDay, '';

var_dump(diffDate($day1, $curDay));

/**
 * function：计算两个日期 相隔 Y年，m月，d天,H小时,相隔总天数a
 * param string $date1[格式如：2011-11-5]
 * param string $date2[格式如：2012-12-01]
 * return array array('年','月','日');
 */
function diffDate($date1,$date2)
{
    $datetime1 = new \DateTime($date1);
    $datetime2 = new \DateTime($date2);
    $interval = $datetime1->diff($datetime2);
    $time['y']         = $interval->format('%Y');
    $time['m']         = $interval->format('%m');
    $time['d']         = $interval->format('%d');
    $time['h']         = $interval->format('%H');
    $time['i']         = $interval->format('%i');
    $time['s']         = $interval->format('%s');
    $time['a']         = $interval->format('%a');    // 两个时间相差总天数
    return $time;
}



